package com.jzg.jcptadmin.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.StringUtils;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.ui.ChooseResultActivity;
import com.jzg.jcptadmin.ui.EvaluationActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jzg on 2016/2/29.
 */
public class SpotPriceFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.et_shouchejia)
    EditText etShouchejia;
    @Bind(R.id.detection_result_edit)
    TextView result;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.tv_toast)
    TextView tvToast;
    @Bind(R.id.surface_des_edit)
    EditText priceC2B;

    private EvaluationActivity evaluationActivity;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View RootView = View.inflate(getActivity(), R.layout.spot_price_layout, null);
        ButterKnife.bind(this, RootView);
        EventBus.getDefault().register(this);
        return RootView;
        //    @Bind(R.id.surface_des_edit)
//    EditText priceC2B;//收车价
//    @Bind(R.id.detection_result_edit)
//    TextView result;
////    @Bind(R.id.et_shouchejia)
////    EditText etShouchejia;
//    @Bind(R.id.tv_toast)
//    TextView tvToast;
//    @Bind(R.id.et_shouchejia)
//    EditText etShouchejia;//车商售车价
    }

    private void initData() {
        result.setOnClickListener(this);
        result.setMovementMethod(new ScrollingMovementMethod());
        result.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    //通知父控件不要干扰
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    //通知父控件不要干扰
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });
        StringUtils.priceTextWatcher(priceC2B, AppContext.getContext());
        StringUtils.priceTextWatcher(etShouchejia, AppContext.getContext());
        evaluationActivity = (EvaluationActivity) getActivity();

        if (TextUtils.isEmpty(evaluationActivity.details.getAppraiseDes())) {
            tvToast.setVisibility(View.VISIBLE);
            count.setVisibility(View.GONE);
        } else {
            tvToast.setVisibility(View.GONE);
            count.setVisibility(View.VISIBLE);
        }
        result.setText(evaluationActivity.details.getAppraiseDes());
        //  String strB2C = StringUtils.getPrice(evaluationActivity.details.getSalePriceB2C());//车商售车价
        // String strC2B = StringUtils.getPrice(evaluationActivity.details.getSalePriceC2B());//车商收车价

        priceC2B.setText(StringUtils.getPrice1(evaluationActivity.details.getSalePriceC2B()));//车商收车价
        etShouchejia.setText(StringUtils.getPrice1(evaluationActivity.details.getSalePriceB2C()));//车商售车价
        evaluationActivity.setEditText(priceC2B, result, etShouchejia);
        //解决EditText跟ScrollView滑动冲突
//        result.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                //这句话说的意思告诉父View我自己的事件我自己处理
//                v.getParent().requestDisallowInterceptTouchEvent(true);
//                return false;
//            }
//        });
        //统计比例
//        String s = result.getText().toString().trim();
//        if (s.length() > 0) {
//            count.setText(s.length() + "/200");
//        }
//        result.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.length() > 200) {
//                    MyToast.showShort("最多输入200字");
//                    return;
//                }
//                count.setText(s.length() + "/200");
//            }
//        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN,priority = 10)
    public void OnEvent(String results) {
        if (TextUtils.isEmpty(results)) {
            tvToast.setVisibility(View.VISIBLE);
            count.setVisibility(View.GONE);
        } else {
            tvToast.setVisibility(View.GONE);
            count.setVisibility(View.VISIBLE);
        }
        result.setText(results);
    }
    @Override
    public void onPause() {
        super.onPause();
       /* InputMethodManager imm = getActivity().(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(price.getWindowToken(), 0);*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detection_result_edit:
                Intent intent = new Intent(getActivity(), ChooseResultActivity.class);
                intent.putExtra("carId",evaluationActivity.details.getCarId());
                intent.putExtra("isNewEighteen",evaluationActivity.details.getIsNewEighteen());
                if(evaluationActivity.details.getEvaluateResult()!=null&&evaluationActivity.details.getEvaluateResult().getSelectedItem().size()>0){
                    intent.putExtra("evaluateResult",evaluationActivity.details.getEvaluateResult());
                }
                startActivity(intent);
                break;
            default:

                break;
        }
    }
}
